package com.zy.dp.composite.example1;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Component
{
	private List<Component> childComponents = null;

	@Override
	public void someOperation()
	{
		if (childComponents != null)
		{
			for (Component c : childComponents)
			{
				c.someOperation();
			}

		}

	}

	public void addChild(Component child)
	{

		if (childComponents == null)
		{
			childComponents = new ArrayList<Component>();
		}

		childComponents.add(child);

	}

	public void removeChild(Component child)
	{

		if (childComponents != null)
		{
			childComponents.remove(child);
		}

	}

	public Component getChildren(int index)
	{

		if (childComponents != null)
		{
			if (index >= 0 && index < childComponents.size())
			{
				return childComponents.get(index);
			}
		}

		return null;

	}

}
